package com.cola.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.cola.common.CookieConstant;
import com.cola.entity.User;
import com.cola.utils.CookieUtils;
import com.cola.utils.jedis.JedisCacheDB;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 * 登录拦截器
 * @author Max
 */
public class LoginInterceptor extends AbstractInterceptor  {
	private static final Logger logger = Logger.getLogger(LoginInterceptor.class);
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		String redisKey = CookieUtils.getCookieValue(ServletActionContext.getRequest()
				, CookieConstant.COLA_WX_ORDERING_SYSTEM_KEY);

		String type = CookieUtils.getCookieValue(ServletActionContext.getRequest()
				, CookieConstant.COLA_WX_ORDERING_SYSTEM_LOGIN_TYPE);

		if(StringUtils.isNotEmpty(redisKey) && StringUtils.isNotEmpty(type)){

			Object redisInfo = JedisCacheDB.getObject(redisKey);

			if(redisInfo!=null){
				User user = JSONObject.parseObject(redisInfo.toString(),User.class);

				if(user.getMobile()!=null && user.getWeChatInfo()!=null
						&& user.getUserId()!=null){
					return invocation.invoke();
				}
			}
		}
		ServletActionContext.getRequest().setAttribute("error_msg","登陆已过期，请重新登录！");
		return Action.LOGIN;
	}

}
